package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.client.ManagementClient;
import com.woniu.command.Command;
import com.woniu.command.orderCommand.AddOrderDeliverCommand;
import com.woniu.command.orderCommand.DeleteOrderDeliverCommand;
import com.woniu.command.orderCommand.UpdateOrderDeliverCommand;
import com.woniu.inlet.vo.CustomerVo;
import com.woniu.outlet.po.OrderDeliver;
import com.woniu.service.OrderDeliverService;
import com.woniu.util.ResponseResult;
import com.woniu.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "送货管理")
@RequestMapping("/order/")
public class OrderDeliverController {

    @Autowired
    private OrderDeliverService orderDeliverService;

    @Autowired
    @Qualifier("orderManagement")
    private ManagementClient managementClient;

    @GetMapping("getCustomerByIds")
    @ApiOperation("根据id查询客户")
    public ResponseResult<CustomerVo> getCustomerByIds(Long id){
        CustomerVo customerVo = managementClient.getCustomerById(id).getData();
        return new ResponseResult<>(customerVo,"查询客户成功", ResultCode.SUCCESS.getCode());
    }

    //查询所有送货信息
    @GetMapping("findOrderDeliver")
    @ApiOperation("查询全部送货信息")
    public ResponseResult<List<OrderDeliver>> findOrderDeliver() throws Exception{
        List<OrderDeliver> orderDeliver = orderDeliverService.findOrderDeliver();
        return new ResponseResult<>(orderDeliver, "OK", ResultCode.SUCCESS.getCode());
    }

    //查询送货信息(根据订单编号)
    @ApiOperation("查询送货信息(根据订单编号)")
    @GetMapping("findOrderDeliverByOrderNo")
    public ResponseResult<OrderDeliver> findOrderDeliverByOrderNo(String orderNo) throws Exception{
        OrderDeliver orderDeliver = orderDeliverService.findOrderDeliverByOrderNo(orderNo);
        return new ResponseResult<>(orderDeliver,"OK", ResultCode.SUCCESS.getCode());
    }

    //添加送货信息
    @PostMapping("addOrderDeliver")
    @ApiOperation("添加送货信息")
    @ApiOperationSupport(ignoreParameters = {"id","orderId","deliveryDate","arrivalDate","spare1","spare2","spare3"})
    public ResponseResult<String> addOrderDeliver(@Valid OrderDeliver orderDeliver, BindingResult result){
        if(result.hasErrors()){
            return new ResponseResult<>(ResultCode.PARAM_TYPE_ERROR.getCode(),ResultCode.PARAM_TYPE_ERROR.getMessage(),"添加失败");
        }
        Command command = new AddOrderDeliverCommand();

        boolean flag = (Boolean) command.excute(orderDeliver);
        if(flag){
            return new ResponseResult<>( ResultCode.SUCCESS.getCode(),"添加送货信息成功");
        }
        return new ResponseResult<>(ResultCode.USER_ACCOUNT_USE_BY_OTHERS.getCode(),ResultCode.USER_ACCOUNT_USE_BY_OTHERS.getMessage(),"添加送货失败");
    }

    //修改送货信息
    @PostMapping("updateOrderDeliver")
    @ApiOperation("修改送货信息")
    @ApiOperationSupport(ignoreParameters = {"deliveryDate","arrivalDate","orderId"})
    public ResponseResult<String> updateOrderDeliver(@Valid OrderDeliver orderDeliver){
        Command command = new UpdateOrderDeliverCommand();
        boolean flag = (Boolean)command.excute(orderDeliver);
        if(flag){
            return new ResponseResult<>( ResultCode.SUCCESS.getCode(),"修改送货信息成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"修改送货信息失败");
    }

    //删除送货信息
    @DeleteMapping("deleteOrderDeliverByOrderNo")
    @ApiOperation("删除送货信息(根据订单编号)")
    public ResponseResult<String> deleteOrderDeliverByOrderNo(String OrderNo){
        Command command = new DeleteOrderDeliverCommand();
        boolean flag = (Boolean)command.excute(OrderNo);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"删除送货信息成功");
        }
        return new ResponseResult<>(ResultCode.ORDERNO_IS_NOT_EXIST.getCode(),ResultCode.ORDERNO_IS_NOT_EXIST.getMessage(),"删除送货信息失败");
    }
}
